<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('includes/functions/functions.php');

class ProductsSearchSubComponent
{
	function ProductsSearchSubComponent()
	{
	}

	function query(&$params) 
	{
		$handle = prepare_input($params['handle']);
		$product_id = (int)$params['productId'];
		$language_id = (int)$params['languageId'];
		$manufacturer_id = (int)$params['manufacturerId'];
		$account_id = (int)$params['accountId'];
		$review_id = (int)$params['reviewId'];

		$search_cart = (boolean)$params['searchCart'];
		$search_also = (boolean)$params['searchAlso'];
		$search_xsell = (boolean)$params['searchXsell'];
		$search_reviews = (boolean)$params['searchReviews'];
		$search_specials = (boolean)$params['searchSpecials'];
		$search_upcoming = (boolean)$params['searchUpcoming'];
		$search_orders = (boolean)$params['searchOrders'];

		$category_id = (int)$params['categoryId'];
		$include_subcategories = (boolean)$params['inc_subcat'];

		$price_from = (double)$params['pfrom'];
		$price_to   = (double)$params['pto'];

		$date_from = (int)$params['dfrom'];
		$date_to   = (int)$params['dto'];

		$keywords = prepare_input($params['keywords']);
		$descriptionSearch = (boolean)$params['desc_search'];

		$offset = (int)$params['offset'];

		$and_where = prepare_input($params['and_where']);
		$input_orderby = prepare_input($params['orderby']);

		$return_details = (boolean)$params['return_details'];
		$return_page_info = (boolean)$params['return_page_info'];

		$max_rows = (int)$params['max_rows'];

		$include_all_status = (boolean)$params['include_all_status'];
		
		$downloadEnabled = returnBoolean(DOWNLOAD_ENABLED);

		$return_download_if_no_filename=(boolean)$params['return_download_if_no_filename'];

		$select = 'select p.products_id, p.products_status, p.products_virtual, pd.products_name, p.products_model, p.products_image, p.products_price, ' .
		'p.manufacturers_id, p.products_quantity, p.products_qty_blocks, p.products_tax_class_id, p.products_weight, ' .
		"DATE_FORMAT(p.products_date_added, '%Y-%m-%d %H:%i:%s') as date_added, " .
		"DATE_FORMAT(p.products_last_modified, '%Y-%m-%d %H:%i:%s') as date_modified, " . 
		"DATE_FORMAT(p.products_date_available, '%Y-%m-%d %H:%i:%s') as date_available, " .
		'p2c.categories_id, p2dc.discount_categories_id, pa.products_attributes_id, ' .
		'ljr.reviews_id as ljr_reviews_id, count(ljr.reviews_id) as count_reviews, sum(ljr.reviews_rating) as sum_reviews, ' .
		'coalesce(min(sp.specials_new_products_price),p.products_price) as special_price, ' .
		'coalesce(min(pb.products_price),p.products_price) as discount_price ';
		
		if ($return_details) 
		{
			$select .= ', pd.products_description, pd.products_url, p.product_image_1, p.product_image_2, p.product_image_3, p.product_image_4, p.product_image_5, p.product_image_6, p.products_image_max_width, p.products_image_max_height ';
		}

		$from = TABLE_PRODUCTS_DESCRIPTION . ' pd, ' . TABLE_PRODUCTS_TO_CATEGORIES . ' p2c, ' . TABLE_CATEGORIES . ' c, ' . TABLE_PRODUCTS . ' p ' .
		'left join ' . TABLE_PRODUCTS_PRICE_BREAK . ' pb on (p.products_id = pb.products_id) ' .
		'left join ' . TABLE_PRODUCTS_TO_DISCOUNT_CATEGORIES . ' p2dc on (p.products_id = p2dc.products_id) ' .
		'left join ' . TABLE_PRODUCTS_ATTRIBUTES . ' pa on (pa.products_id = p.products_id) ' .
		'left join ' . TABLE_REVIEWS . ' ljr on (ljr.products_id = p.products_id) ';

		$leftjoin_specials = 'left join ' . TABLE_SPECIALS . ' sp on (p.products_id = sp.products_id and sp.status = \'1\' and ((now() < sp.expires_date and sp.expires_date > 0) or sp.expires_date = 0)) ';

		$where = 'where ';

		$groupby = 'group by p.products_id ';

		$orderby = 'order by pd.products_name';

		if ($search_cart)
		{
			$select .= ', cb.customers_basket_quantity, cb.products_id as cart_products_id ';
		
			$from = 'from ' . TABLE_CUSTOMERS_BASKET . ' cb, ' . $from . $leftjoin_specials;
			
			$where .= 'cb.customers_id = \'' . $account_id . '\' and p.products_id=cb.products_id_actual and ';

			$groupby = 'group by cb.products_id ';
		}
		else if ($search_also)
		{
			$from = 'from ' . TABLE_ORDERS_PRODUCTS . ' opa, ' . TABLE_ORDERS_PRODUCTS . ' opb, ' . TABLE_ORDERS . ' o, ' . $from . $leftjoin_specials;
			
			$where .= 'opa.products_id = \'' . $product_id . '\' ' .
			'and opa.orders_id = opb.orders_id and opb.products_id != \'' . $product_id . '\' ' . 
			'and opb.products_id = p.products_id and opb.orders_id = o.orders_id  and o.orders_status > -1 and ';

			$orderby = 'order by o.date_purchased desc';
		}
		else if ($search_xsell)
		{
			$from = 'from ' . TABLE_PRODUCTS_XSELL . ' px, ' . $from . $leftjoin_specials;

			$where .= 'p.products_id = px.xsell_id and px.products_id = \'' . $product_id . '\' and ';

			$orderby = 'order by p.products_date_added desc';
		}
		else if ($search_reviews)
		{
			$select .= ', r.reviews_id, r.customers_id, r.customers_name, r.reviews_rating, r.reviews_read, rd.reviews_text, ' .
			"DATE_FORMAT(r.date_added, '%Y-%m-%d %H:%i:%s') as review_date_added, DATE_FORMAT(r.last_modified, '%Y-%m-%d %H:%i:%s') as review_date_modified ";
		
			$from = 'from ' . TABLE_REVIEWS . ' r, ' . TABLE_REVIEWS_DESCRIPTION . ' rd, ' . $from . $leftjoin_specials;
			
			$where .= 'r.products_id = p.products_id and r.reviews_id = rd.reviews_id and rd.languages_id = \'' . $language_id . '\' and ';

			if ($review_id > 0) $where .= 'r.customers_id = ' . $account_id . ' and r.reviews_id = ' . $review_id . ' and ';

			$groupby = 'group by r.reviews_id ';
		}
		else if ($search_specials)
		{
			$select .= ", sp.specials_id, sp.status as specials_status, DATE_FORMAT(sp.expires_date, '%Y-%m-%d %H:%i:%s') as specials_expires_date ";
			$from = 'from ' . TABLE_SPECIALS . ' sp, ' . $from;
			$where .= 'p.products_id = sp.products_id and ';
			if (!$include_all_status)
				$where .= 'sp.status = \'1\' and ((now() < sp.expires_date and sp.expires_date > 0) or sp.expires_date = 0) and ';
		}
		else if ($search_upcoming)
		{
			$from = 'from ' . $from . $leftjoin_specials;

			$where .= 'to_days(p.products_date_available) >= to_days(now()) and ';

			$orderby = 'order by p.products_date_available desc';
		}
		else if ($search_orders)
		{
			$from = 'from ' . TABLE_ORDERS_PRODUCTS . ' op, ' . TABLE_ORDERS . ' o, ' . $from . $leftjoin_specials;
			
			$where .= 'o.customers_id = \'' . $account_id . '\' ' . 
			'and op.products_id = p.products_id and op.orders_id = o.orders_id and o.orders_status > -1 and ';

			$orderby = 'order by pd.products_name';
		}
		else
		{
			$from = 'from ' . $from . $leftjoin_specials;
		}
		
		$status = 1;
		if ($search_upcoming) $status = 0;

		$where .= 'p.products_id = pd.products_id and pd.language_id = \'' . $language_id . '\' ' .
		'and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id ';

		if (!$include_all_status) $where .= 'and p.products_status = \'' . $status . '\'  ';

		if ($product_id > 0 && !$search_xsell && !$search_also && !$search_orders)
		{
			$where .= 'and p.products_id = \'' . $product_id . '\' ';
		}

		if ($category_id > 0)
		{
			if ($include_subcategories)
			{
				$where .= 'and \'' . $category_id . '\' in (c.categories_id, c.parent_id) ';
			}
			else
			{	
				$where .= 'and p2c.categories_id = \'' . $category_id . '\' ';
			}
		}

		if ($manufacturer_id > 0) {
			$where .= 'and p.manufacturers_id = \'' . $manufacturer_id . '\' ';
		}

		if ($price_from > 0) $where .= 'and (if(coalesce(sp.specials_new_products_price,p.products_price) < coalesce(pb.products_price,p.products_price), ' .
		'coalesce(sp.specials_new_products_price,p.products_price), coalesce(pb.products_price,p.products_price)) >= ' . $price_from . ') ';

		if ($price_to > 0)   $where .= 'and coalesce(sp.specials_new_products_price,p.products_price) <= ' . $price_to . ' ';
			
		
		if ($date_from > 0) 
		{
			$date_from_sec = (int)$date_from/1000.00;
			$where .= 'and p.products_date_added >= \'' . date('Y-m-d',$date_from_sec) . '\' ';
		}

		if ($date_to > 0)
		{
			$date_to_sec = (int)$date_to/1000.00;
			$where .= 'and p.products_date_added <= \'' . date('Y-m-d',$date_to_sec) . '\' ';
		}

		if ($keywords != null)
		{
			if (!parse_search_string($keywords, $search_keywords))
			{
				//TBD add error to metadata
			}

			if (isset($search_keywords) && (sizeof($search_keywords) > 0))
			{
				$where .= ' and (';
				for ($i=0, $n=sizeof($search_keywords); $i<$n; $i++ )
				{
					switch ($search_keywords[$i]) {
						case '(':
						case ')':
						case 'and':
						case 'or':
							$where .= ' ' . mysql_real_escape_string(prepare_input($search_keywords[$i])) . ' ';
							break;
						default:
							$keyword = mysql_real_escape_string(prepare_input($search_keywords[$i]));
							$where .= '(pd.products_name like \'%' . $keyword . '%\' ' .
							'or p.products_model like \'%' . $keyword . '%\'';
							if ($descriptionSearch)
							{
								$where .= ' or pd.products_description like \'%' . $keyword . '%\'';
							}
							$where .= ')';
							break;
					}
				}
				$where .= ') ';
			}
		}
		
		if ($and_where != null && $and_where != '')
		{
			$where .= $and_where . ' ';
		}
		if ($input_orderby != null && $input_orderby != '')
		{
			$orderby = 'order by ' . mysql_real_escape_string($input_orderby);
		}
		$value{$handle . 'MetaData'}{'display_cart'} = returnBoolean(DISPLAY_CART);
		$value{$handle . 'MetaData'}{'image_width_max'}= (int)SMALL_IMAGE_WIDTH;
		$value{$handle . 'MetaData'}{'image_height_max'}= (int)SMALL_IMAGE_HEIGHT;
		$value{$handle . 'MetaData'}{'qty_in_cart_max'} = (int)MAX_QTY_IN_CART;
		$value{$handle . 'MetaData'}{'return_details'}= $return_details;
		$value{$handle . 'MetaData'}{'return_page_info'}= $return_page_info;
		$value{$handle . 'MetaData'}{'warn_if_out_of_stock'}= STOCK_MARK_PRODUCT_OUT_OF_STOCK;
		if ($search_reviews)
		{
			$value{$handle . 'MetaData'}{'review_text_min'} = (int)REVIEW_TEXT_MIN_LENGTH;
		}

		$sqlQuery = $select . $from . $where . $groupby . $orderby . ' limit ' . $offset . ', ' . $max_rows;
//var_dump($sqlQuery);
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());

		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			if ($search_cart)
			{
				$value{$handle}{$i}{'cart_qty'}= (int)$row['customers_basket_quantity'];
				$cart_product_id = $row['cart_products_id'];
				$value{$handle}{$i}{'cart_products_id'} = $cart_product_id;
			}
			else if ($search_reviews)
			{
				$value{$handle}{$i}{'reviews_id'}= (int)$row['reviews_id'];
				$value{$handle}{$i}{'account_id'}= (int)$row['customers_id'];
				$value{$handle}{$i}{'account_name'}= $row['customers_name'];
				$value{$handle}{$i}{'rating'}= (int)$row['reviews_rating'];
				$value{$handle}{$i}{'review_date_added'}= $row['review_date_added'];
				$value{$handle}{$i}{'review_date_modified'}= $row['review_date_modified'];
				$value{$handle}{$i}{'review_text'}= $row['reviews_text'];
			}
			else if ($search_specials)
			{
				$value{$handle}{$i}{'special_id'}= (int)$row['specials_id'];
				$value{$handle}{$i}{'special_status'}= (boolean)$row['specials_status'];
				$value{$handle}{$i}{'special_expires_date'}= $row['specials_expires_date'];
			}

			$product_id = (int)$row['products_id'];
			$value{$handle}{$i}{'id'}= $product_id;
			$value{$handle}{$i}{'categories_id'}= (int)$row['categories_id'];
			$value{$handle}{$i}{'date_added'}= $row['date_added'];
			$value{$handle}{$i}{'date_modified'}= $row['date_modified'];
			$value{$handle}{$i}{'date_available'}= $row['date_available'];
			$value{$handle}{$i}{'discount_categories_id'}= (int)$row['discount_categories_id'];
			$value{$handle}{$i}{'discount_price'}= (double)$row['discount_price'];

			$value{$handle}{$i}{'inventory_qty'}= (int)$row['products_quantity'];

			if ($return_details)
			{
				$width_max = (int)$row['products_image_max_width'];
				$height_max = (int)$row['products_image_max_height'];
				if ($width_max == 0) $width_max = (int)SMALL_IMAGE_WIDTH;
				if ($height_max == 0) $height_max = (int)SMALL_IMAGE_HEIGHT;
				$value{$handle}{$i}{'image_width_max'}=$width_max;
				$value{$handle}{$i}{'image_height_max'}=$height_max;
			}
			else {
				$width_max = (int)SMALL_IMAGE_WIDTH;
				$height_max = (int)SMALL_IMAGE_HEIGHT;
			}

			$imagename = $row['products_image'];
			list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
			$value{$handle}{$i}{'image'}= $imagename;
			$value{$handle}{$i}{'image_width'}= $imagewidth;
			$value{$handle}{$i}{'image_height'}= $imageheight;
			
			$value{$handle}{$i}{'manufacturer_id'}= (int)$row['manufacturers_id'];
			$value{$handle}{$i}{'model'}= $row['products_model'];
			$value{$handle}{$i}{'name'}= $row['products_name'];
			$value{$handle}{$i}{'status'}= (boolean)$row['products_status'];
			$value{$handle}{$i}{'virtual'}= (boolean)$row['products_virtual'];
			$value{$handle}{$i}{'price'}= (double)$row['products_price'];
			$value{$handle}{$i}{'qty_blocks'}= (int)$row['products_qty_blocks'];
			$value{$handle}{$i}{'special_price'}= (double)$row['special_price'];
			$value{$handle}{$i}{'tax_class_id'}= (int)$row['products_tax_class_id'];
			$value{$handle}{$i}{'weight'}= (double)$row['products_weight'];

			$attributes = false;
			if ((int)$row['products_attributes_id'] > 0) $attributes = true;
			$value{$handle}{$i}{'products_attributes'}= $attributes;

			$reviews = false;
			if ((int)$row['ljr_reviews_id'] > 0) $reviews = true;
			$value{$handle}{$i}{'products_reviews'}= $reviews;
			$count_reviews = (int)$row['count_reviews'];
			$value{$handle}{$i}{'count_reviews'}= $count_reviews;
			if ($count_reviews > 0)	{
				$sum_reviews = (double)$row['sum_reviews'];
				$value{$handle}{$i}{'reviews_average'}=$sum_reviews/$count_reviews;
			}
			if ($return_details)
			{
				$value{$handle}{$i}{'description'}= $row['products_description'];
				$value{$handle}{$i}{'url'}= $row['products_url'];

				$imagename = $row['product_image_1'];
				list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
				$value{$handle}{$i}{'image_1'}= $imagename;
				$value{$handle}{$i}{'image_width_1'}= $imagewidth;
				$value{$handle}{$i}{'image_height_1'}= $imageheight;

				$imagename = $row['product_image_2'];
				list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
				$value{$handle}{$i}{'image_2'}= $imagename;
				$value{$handle}{$i}{'image_width_2'}= $imagewidth;
				$value{$handle}{$i}{'image_height_2'}= $imageheight;

				$imagename = $row['product_image_3'];
				list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
				$value{$handle}{$i}{'image_3'}= $imagename;
				$value{$handle}{$i}{'image_width_3'}= $imagewidth;
				$value{$handle}{$i}{'image_height_3'}= $imageheight;

				$imagename = $row['product_image_4'];
				list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
				$value{$handle}{$i}{'image_4'}= $imagename;
				$value{$handle}{$i}{'image_width_4'}= $imagewidth;
				$value{$handle}{$i}{'image_height_4'}= $imageheight;

				$imagename = $row['product_image_5'];
				list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
				$value{$handle}{$i}{'image_5'}= $imagename;
				$value{$handle}{$i}{'image_width_5'}= $imagewidth;
				$value{$handle}{$i}{'image_height_5'}= $imageheight;

				$imagename = $row['product_image_6'];
				list($imagename,$imagewidth, $imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
				$value{$handle}{$i}{'image_6'}= $imagename;
				$value{$handle}{$i}{'image_width_6'}= $imagewidth;
				$value{$handle}{$i}{'image_height_6'}= $imageheight;

				$sqlQuery1 = 'select products_price, products_qty ' .
				'from  ' . TABLE_PRODUCTS_PRICE_BREAK . ' ' .
				'where products_id = \'' . $product_id . '\' ' .
				'order by products_qty';

				$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());

				$i1 = 0;
				while($row1 = mysql_fetch_array($dataReturned1))
				{
					$value{$handle}{$i}{'price_breaks'}{$i1}{'price'}= (double)$row1['products_price'];
					$value{$handle}{$i}{'price_breaks'}{$i1}{'quantity'}= (int)$row1['products_qty'];
					$i1++;
				}
			
				$sqlQuery1 = 'select  pa.products_attributes_id, po.products_options_id, po.products_options_name, pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix';
				
				if ($downloadEnabled) {
					$sqlQuery1 .= ', pad.products_attributes_filename, pad.products_attributes_maxdays, pad.products_attributes_maxcount';
				}

				$sqlQuery1 .= ' from ' . TABLE_PRODUCTS_OPTIONS . ' po, ' . TABLE_PRODUCTS_OPTIONS_VALUES . ' pov, ' . TABLE_PRODUCTS_ATTRIBUTES . ' pa '; 

				if ($downloadEnabled) {
					$sqlQuery1 .= 'left join ' . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . ' pad on (pa.products_attributes_id = pad.products_attributes_id) ';
				}

				$sqlQuery1 .= 'where pa.products_id = \'' . $product_id . '\' ' .
				'and pa.options_id = po.products_options_id and po.language_id = \'' . $language_id . '\' ' .
				'and pa.options_values_id = pov.products_options_values_id and pov.language_id = \'' . $language_id . '\' ' .
				'order by products_options_name, pa.attribute_sort, pov.products_options_values_name';

				$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());

				$i1 = 0;
				while($row1 = mysql_fetch_array($dataReturned1))
				{
					$values_name_extension = '';
					if ($downloadEnabled) {
						$filename = $row1['products_attributes_filename'];
						if ($filename != null && strlen($filename) > 0) {
							$filepath = DIR_FS_DOWNLOAD . $filename;
							if (!$return_download_if_no_filename && !file_exists($filepath)) continue; //Don't show option
							$value{$handle}{$i}{'attributes'}{$i1}{'filename'}= $filename;
							$value{$handle}{$i}{'attributes'}{$i1}{'maxdays'}= (int)$row1['products_attributes_maxdays'];
							$value{$handle}{$i}{'attributes'}{$i1}{'maxcount'}= (int)$row1['products_attributes_maxcount'];

							if (file_exists($filepath)) {
								$image_size = @getimagesize($filepath);
								$file_size = formatBytes((int)trim(`stat -c%s $filepath`));
								$values_name_extension = ' [' . $image_size[0] . 'px wide by ' . $image_size[1] . 'px high: ' . $file_size . ']';
							}
						}
					}
					$value{$handle}{$i}{'attributes'}{$i1}{'row_id'}= (int)$row1['products_attributes_id'];
					$value{$handle}{$i}{'attributes'}{$i1}{'product_id'}= $product_id;
					$value{$handle}{$i}{'attributes'}{$i1}{'id'}= (int)$row1['products_options_id'];
					$value{$handle}{$i}{'attributes'}{$i1}{'name'}= $row1['products_options_name'];
					$value{$handle}{$i}{'attributes'}{$i1}{'values_id'}= (int)$row1['products_options_values_id'];
					$value{$handle}{$i}{'attributes'}{$i1}{'values_name'}= $row1['products_options_values_name'] . $values_name_extension;
					$value{$handle}{$i}{'attributes'}{$i1}{'values_price'}= (double)$row1['options_values_price'];
					$value{$handle}{$i}{'attributes'}{$i1}{'values_price_prefix'}= $row1['price_prefix'];
					$i1++;
				}
			}
			else if ($search_cart)
			{
				$value{$handle}{$i}{'description'}= '';
				$value{$handle}{$i}{'url'}= '';

				$sqlQuery1 = 'select products_price, products_qty ' .
				'from  ' . TABLE_PRODUCTS_PRICE_BREAK . ' ' .
				'where products_id = \'' . $product_id . '\' ' .
				'order by products_qty';

				$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());

				$i1 = 0;
				while($row1 = mysql_fetch_array($dataReturned1))
				{
					$value{$handle}{$i}{'price_breaks'}{$i1}{'price'}= (double)$row1['products_price'];
					$value{$handle}{$i}{'price_breaks'}{$i1}{'quantity'}= (int)$row1['products_qty'];
					$i1++;
				}

				$sqlQuery1 = 'select po.products_options_id, po.products_options_name, pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix';
				
				if ($downloadEnabled) {
					$sqlQuery1 .= ', pad.products_attributes_filename, pad.products_attributes_maxdays, pad.products_attributes_maxcount';
				}

				$sqlQuery1 .= ' from ' . TABLE_CUSTOMERS_BASKET . ' cb, ' . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . ' cba, ' . 
				TABLE_PRODUCTS_OPTIONS . ' po, ' . TABLE_PRODUCTS_OPTIONS_VALUES . ' pov, ' . TABLE_PRODUCTS_ATTRIBUTES . ' pa ';
				
				if ($downloadEnabled) {
					$sqlQuery1 .= 'left join ' . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . ' pad on (pa.products_attributes_id = pad.products_attributes_id) ';
				}
					
				$sqlQuery1 .= 'where cb.customers_id = \'' . $account_id .'\' and  cba.customers_id = \'' . $account_id .'\' ' .
				'and cb.products_id_actual = pa.products_id and cb.products_id = cba.products_id ' . 
				'and cba.products_options_id = pa.options_id and cba.products_options_value_id = pa.options_values_id ' .
				'and cb.products_id = \'' . $cart_product_id . '\' ' .
				'and pa.options_id = po.products_options_id and po.language_id = \'' . $language_id . '\' ' .
				'and pa.options_values_id = pov.products_options_values_id and pov.language_id = \'' . $language_id . '\' ' .
				'order by products_options_name, pa.attribute_sort, pov.products_options_values_name';

				$dataReturned1 = mysql_query($sqlQuery1) or die(mysql_error());
				$i1 = 0;
				while($row1 = mysql_fetch_array($dataReturned1))
				{
					$values_name_extension = '';
					if ($downloadEnabled) {
						$filename = $row1['products_attributes_filename'];
						if ($filename != null && strlen($filename) > 0) {
							$filepath = DIR_FS_DOWNLOAD . $filename;
							$value{$handle}{$i}{'cart_options'}{$i1}{'filename'}= $filename;
							$value{$handle}{$i}{'cart_options'}{$i1}{'maxdays'}= (int)$row1['products_attributes_maxdays'];
							$value{$handle}{$i}{'cart_options'}{$i1}{'maxcount'}= (int)$row1['products_attributes_maxcount'];
							if (file_exists($filepath)) {
								$image_size = @getimagesize($filepath);
								$file_size = formatBytes((int)trim(`stat -c%s $filepath`));
								$values_name_extension = ' [' . $image_size[0] . 'px wide by ' . $image_size[1] . 'px high: ' . $file_size . ']';
							}
							else {
								$values_name_extension = 'Missing file!';
							}
						}
					}
					$value{$handle}{$i}{'cart_options'}{$i1}{'product_id'}= $product_id;
					$value{$handle}{$i}{'cart_options'}{$i1}{'id'}= (int)$row1['products_options_id'];
					$value{$handle}{$i}{'cart_options'}{$i1}{'name'}= $row1['products_options_name'];
					$value{$handle}{$i}{'cart_options'}{$i1}{'values_id'}= (int)$row1['products_options_values_id'];
					$value{$handle}{$i}{'cart_options'}{$i1}{'values_name'}= $row1['products_options_values_name'] . $values_name_extension;
					$value{$handle}{$i}{'cart_options'}{$i1}{'values_price'}= (double)$row1['options_values_price'];
					$value{$handle}{$i}{'cart_options'}{$i1}{'values_price_prefix'}= $row1['price_prefix'];
					
					
					$i1++;
				}
			}
			else
			{
				$value{$handle}{$i}{'description'}= '';
				$value{$handle}{$i}{'url'}= '';
			}

			$i++;
		}

		if ($return_page_info)
		{
			$sqlQuery = 'select p.products_id ' . $from . $where . $groupby;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = (int) mysql_num_rows ($dataReturned);
			$value{$handle . 'MetaData'}{'max_rows'}= (int)$max_rows;
			$value{$handle . 'MetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{$handle . 'MetaData'}{'prev_next_bar_location'} = (int)PREV_NEXT_BAR_LOCATION;
			$value{$handle . 'MetaData'}{'offset'}= $offset;
			$value{$handle . 'MetaData'}{'rows'}= $i;
			$value{$handle . 'MetaData'}{'total_rows'}= $cnt;
		}
//var_dump($value);
		return $value;
	}
}
?>
